package dao.impl;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class HibernateBase {

	private final String UNIT_NAME = "hibernate";
	private EntityManagerFactory factory;
	private EntityManager manager;
	private static HibernateBase myInstance;

	public static synchronized HibernateBase getInstance() {
		if (myInstance == null) {
			myInstance = new HibernateBase();
		}
		return myInstance;
	}

	public void init() {
		if (this.factory == null || !this.factory.isOpen())
			this.factory = Persistence.createEntityManagerFactory(UNIT_NAME);

		if (this.manager == null || !this.manager.isOpen())
			this.manager = this.factory.createEntityManager();
	}

	public void close() {
		this.manager.close();
	}

	public EntityManager getManager() {
		return manager;
	}

	public void setManager(EntityManager manager) {
		this.manager = manager;
	}
}
